package utils;


import java.io.UnsupportedEncodingException;
import java.util.Arrays;

/**
 * Created by 他的猫 on 2017/5/18.
 */

public class JX_Utils {
    private static final char[] DIGITS_UPPER = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    /**
     * 计价器 16进制asc转 String
     * 例如：ASCII ：BEA94A4C313233343536转成字符串 京JL123456
     * 将16进制ASCII转成字符串
     *
     * @param value 要转化的ASCII
     * @return
     */
    public static String asciiToString2(String value) {
        try {
            return new String(hex2byte(change(value)), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return value;
        }
    }

    public static byte[] hex2byte(String value) {
        String[] str = value.split(" ");
        byte[] b = new byte[str.length];
        for (int i = 0; i < str.length; i++) {
            b[i] = (byte) hexStringToInt(str[i]);
        }
        return b;
    }

    /**
     * 数据解析 把16进制转换出来  038e转换成  1000
     *
     * @param value
     * @return
     */
    public static int hexStringToInt(String value) {
        return Integer.valueOf(value, 16);
    }


    /**
     * 数据解析 1000转换成 038e
     *
     * @param value
     * @return
     */
    public static String intToHexString(int a, int len) {
        len <<= 1;
        String hexString = Integer.toHexString(a);
        int b = len - hexString.length();
        if (b > 0) {
            for (int i = 0; i < b; i++) {
                hexString = "0" + hexString;
            }
        }
        return hexString;
    }


    public static String change(String content) {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < content.length(); i++) {
            if (i % 2 == 0) {
                str.append(" " + content.substring(i, i + 1));
            } else {
                str.append(content.substring(i, i + 1));
            }
        }
        return str.toString().trim();
    }


    /**
     * APP  字符串转16进制Asc码
     * 将字符串装成16进制 ASCII码，例如： 字符串 ：京JL123456  转成16进制ASCII码 BEA94A4C313233343536
     *
     * @param strAsci strAsci要转成ASCII的字符串
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String stringToAsciiToHexString(String strAsci) throws UnsupportedEncodingException {
        String s = String.valueOf(strAsci);
        System.out.println(s.length() + ",valueof=" + s);
        byte[] bytes = s.getBytes("GBK");
        System.out.println(bytes.length + ",bytes=" + Arrays.toString(bytes));
        String strResult = HexToString(bytes);

        return strResult;
    }

    /**
     * 16进制的byte转String
     *
     * @param inHex
     * @return
     */

    public static String HexToString(byte[] inHex) {
        byte i;
        String strResult = "";
        for (i = 0; i < inHex.length; i++) {
            strResult += String.valueOf(DIGITS_UPPER[(0xF0 & inHex[i]) >>> 4]) + String.valueOf(DIGITS_UPPER[inHex[i] & 0x0F]);
            //System.out.println(i+",resultStr="+strResult);
        }
        return strResult;
    }

    /**
     * 计价器   (UINT8) 16进制数据转2进制BYTE每位
     * 将16进制字符串转化成二进制字符串
     *
     * @param hexString 16进制字符串
     * @return
     */
    public static String hexString2binaryString(String hexString)

    {

        if (hexString == null || hexString.length() % 2 != 0)

            return null;

        String bString = "", tmp;

        for (int i = 0; i < hexString.length(); i++)

        {

            tmp = "0000"

                    + Integer.toBinaryString(Integer.parseInt(hexString

                    .substring(i, i + 1), 16));

            bString += tmp.substring(tmp.length() - 4);

        }

        return bString;

    }
    // UINT8  无符号整数 可以直接看  收到是16进制的转成10进制就行
    //UINT32    无符号整数  是4个字节的

    /**
     * 计价器  16进制byte    转Stringz
     * 16进制的Byte字符串转String
     *
     * @param s 16进制字符串
     */

    public static String HexToString(String s) {
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(
                        s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "utf-8");// UTF-16le:Not
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }


    public static String bTString(byte[] mMsgs) {
        int lh = mMsgs[10] * 256;
        int lw = mMsgs[11] & 0xFF;
        int msg_length = lh + lw;
        byte[] byteMsg = new byte[msg_length];
        for (int i = 0; i < byteMsg.length; i++) {
            byteMsg[i] = mMsgs[i + 12];
        }

        String msg = null;
        try {
            msg = new String(byteMsg, "gbk");
        } catch (Exception e) {
            e.printStackTrace();
        }
//        android.util.Log.e("btString", "9999999 " + msg);
        return msg;
    }


    public static String HexToStringGBK(String s) {
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(
                        s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "GBK");// UTF-16le:Not
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }


    /**
     * APP   String转16进制byte
     */

    public static String getAscii(String c) {
        String s = String.valueOf(c);
        byte[] bytes = s.getBytes();
        String dd = "";
        for (int i = 0; i < bytes.length; i++)
            dd += Integer.toHexString(bytes[i] & 0xff);
        return dd;

    }


    public static String bcdToStr(byte[] bytes) {
        StringBuffer temp = new StringBuffer(bytes.length * 2);

        for (int i = 0; i < bytes.length; i++) {
            temp.append((byte) ((bytes[i] & 0xf0) >>> 4));
            temp.append((byte) (bytes[i] & 0x0f));
        }
        return temp.toString().substring(0, 1).equalsIgnoreCase("0") ? temp
                .toString().substring(1) : temp.toString();
    }


    /**
     * 收发蓝牙 串口数据可能会用到
     * byte数组转成16进制字符串  根据16进制的每个字段来判断
     *
     * @param inHex
     * @param nOff
     * @param nLen
     * @return
     */
    public static String BytesToString(byte[] inHex, int nOff, int nLen) {
        int i;
        StringBuilder strResult = new StringBuilder();
        strResult.append("");
        for (i = 0; i < nLen; i++) {
            strResult.append(String.valueOf(DIGITS_UPPER[(0xF0 & inHex[nOff + i]) >>> 4]));
            strResult.append(String.valueOf(DIGITS_UPPER[inHex[nOff + i] & 0x0F]));
        }
        return strResult.toString();
    }
    // 杩涜涓庝綅杩愮畻寰楀埌bcc
    public static String formt(String s) {
        int a = 0;
        for (int i = 0; i < s.length() / 2; i++) {
            String sb = s.substring(i * 2, i * 2 + 2);
            int parseInt = Integer.parseInt(sb, 16);
            a ^= parseInt;
        }
        return int2hexStr(a);
    }
    public static String int2hexStr(int index) {
        String indexStr = Integer.toHexString(index);
        indexStr = indexStr.length() < 2 ? "0" + indexStr : indexStr;
        return indexStr;
    }
}
